package me.zhengjie.wechat.service.impl;

import com.alibaba.fastjson.JSON;
import me.zhengjie.wechat.domain.WechatCode;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.wechat.repository.WechatCodeRepository;
import me.zhengjie.wechat.service.WechatCodeService;
import me.zhengjie.wechat.service.dto.WechatCodeDTO;
import me.zhengjie.wechat.service.dto.WechatCodeQueryCriteria;
import me.zhengjie.wechat.service.mapper.WechatCodeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author Jiang yunxiao
* @date 2019-11-10
*/
@Service
@CacheConfig(cacheNames = "wechatCode")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class WechatCodeServiceImpl implements WechatCodeService {

    private final WechatCodeRepository wechatCodeRepository;

    private final WechatCodeMapper wechatCodeMapper;

    public WechatCodeServiceImpl(WechatCodeRepository wechatCodeRepository, WechatCodeMapper wechatCodeMapper) {
        this.wechatCodeRepository = wechatCodeRepository;
        this.wechatCodeMapper = wechatCodeMapper;
    }

    @Override
  //  @Cacheable
    public Map<String,Object> queryAll(WechatCodeQueryCriteria criteria, Pageable pageable){
        Page<WechatCode> page = wechatCodeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(wechatCodeMapper::toDto));
    }

    @Override
    @Cacheable
    public List<WechatCodeDTO> queryAll(WechatCodeQueryCriteria criteria){
        return wechatCodeMapper.toDto(wechatCodeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Cacheable(key = "#p0")
    public WechatCodeDTO findById(Long id) {
        WechatCode wechatCode = wechatCodeRepository.findById(id).orElseGet(WechatCode::new);
        ValidationUtil.isNull(wechatCode.getId(),"WechatCode","id",id);
        return wechatCodeMapper.toDto(wechatCode);
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public WechatCodeDTO create(WechatCode resources) {
        return wechatCodeMapper.toDto(wechatCodeRepository.save(resources));
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(WechatCode resources) {
        WechatCode wechatCode = wechatCodeRepository.findById(resources.getId()).orElseGet(WechatCode::new);
        ValidationUtil.isNull( wechatCode.getId(),"WechatCode","id",resources.getId());
        wechatCode.copy(resources);
        wechatCodeRepository.save(wechatCode);
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        wechatCodeRepository.deleteById(id);
    }


    @Override
    public void download(List<WechatCodeDTO> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (WechatCodeDTO wechatCode : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("可接广告类型", wechatCode.getAdvertisementType());
            map.put("所属公司", wechatCode.getCompany());
            map.put("创建时间", wechatCode.getCreationTime());
            map.put("客服", wechatCode.getCustomerService());
            map.put("粉丝数量", wechatCode.getFansNumber());
            map.put("男女比例", wechatCode.getProportion());
            map.put("备注", wechatCode.getRemark());
            map.put("类型 ", wechatCode.getType());
            map.put("更新时间",  wechatCode.getUpdateTime());
            map.put("微信号", wechatCode.getWechatCade());
            map.put("公众号名称", wechatCode.getWechatName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}